package com.soft.leo.oldbook;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.panxw.android.imageindicator.AutoPlayManager;
import com.panxw.android.imageindicator.ImageIndicatorView;
import com.soft.leo.oldbook.adapter.MyAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity{
    private ViewPager mainPager;
    private List<View> viewList;
    private ImageButton clubMenu;
    private LinearLayout submit_book_bt;
    private ImageIndicatorView indicate_view;
    LinearLayout indexJumper,clubJumper,messageJumper,userJumper;
    private ListView indexList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //region 浸入式标题栏
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        //endregion
        setContentView(R.layout.activity_main);


        initComp();
        initFunc();
        initPagerBt();
        local();
        initNetRequest();
    }

    private void initNetRequest() {



        new Thread(){
            @Override
            public void run() {
                ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
                HashMap<String,Object> map=new HashMap<>();
                map.put("author","李超");
                map.put("img","https://img3.doubanio.com/mpic/s29522132.jpg");
                map.put("name","备忘录");
                map.put("rating","3.2");
                map.put("rater","254");
                map.put("price","15.8");
                map.put("pubdate","2017-04-15");
                map.put("publisher","青岛出版社");
                list.add(map);
                indexList.setAdapter(new MyAdapter(MainActivity.this,list));
                OkHttpClient client=new OkHttpClient();
                Request request=new Request.Builder().url("https://api.douban.com/v2/book/isbn/9787121319921").build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //Log.i("test",response.body().string());
                        try {
                            JSONObject json=new JSONObject(response.body().string());
                            String demo=json.get("author").toString();
                            String demosub = demo.substring(1,demo.length()-1);
                            String demoArray[] = demosub.split(",");
                            List<String> demoList = Arrays.asList(demoArray);
                            Log.i("test",demoList.get(0));


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }.start();
    }

    private void initPagerBt() {
        indexJumper=(LinearLayout) findViewById(R.id.index_jumper);
        clubJumper=(LinearLayout)findViewById(R.id.club_jumper);
        messageJumper=(LinearLayout)findViewById(R.id.message_jumper);
        userJumper=(LinearLayout)findViewById(R.id.user_jumper);
        indexJumper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainPager.setCurrentItem(0);
                initBtImg(0);
            }
        });
        clubJumper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainPager.setCurrentItem(1);
                initBtImg(1);

            }
        });
        messageJumper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainPager.setCurrentItem(2);
                initBtImg(2);
            }
        });
        userJumper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainPager.setCurrentItem(3);
                initBtImg(3);
            }
        });


    }

    private void initComp() {
        mainPager=(ViewPager)findViewById(R.id.mainpager);
        submit_book_bt=(LinearLayout)findViewById(R.id.submitBookBt);

        viewList=new ArrayList<>();
    }

    private void initFunc() {


        //region 对ViewPager进行功能设置
        LayoutInflater inflater=LayoutInflater.from(this);
        View mainView=inflater.inflate(R.layout.activity_index,null);
        View clubView=inflater.inflate(R.layout.activity_book_club,null);
        View messageView=inflater.inflate(R.layout.activity_message,null);
        View userView=inflater.inflate(R.layout.activity_user,null);

        viewList.add(mainView);
        viewList.add(clubView);
        viewList.add(messageView);
        viewList.add(userView);
        mainPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
        });
        mainPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                initBtImg(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //endregion
        //region 对陈书圈进行组件设置
        clubMenu=(ImageButton)clubView.findViewById(R.id.clubmenu);
        clubMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.club_share_menu, null),
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                mPop.setOutsideTouchable(true);
                mPop.showAsDropDown(view,0,0);


            }
        });
        View subView=getLayoutInflater().inflate(R.layout.book_submit_menu,null);
        final PopupWindow popupWindow=new PopupWindow(subView,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setAnimationStyle(R.style.Animation);
        ((LinearLayout)subView.findViewById(R.id.singleSubmit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,SubmitActivity.class));
            }
        });
        ((LinearLayout)subView.findViewById(R.id.mulSubmit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"教材打包",Toast.LENGTH_SHORT).show();
            }
        });
        ((LinearLayout)subView.findViewById(R.id.otherSubmit)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        //endregion
        submit_book_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                popupWindow.showAtLocation(submit_book_bt, Gravity.BOTTOM,0,100);
            }
        });
        indicate_view = (ImageIndicatorView) mainView.findViewById(R.id.indicate_view);
        indexList=(ListView)mainView.findViewById(R.id.indexlist);

    }
    private void initBtImg(int index){
        indexJumper.setBackgroundColor(Color.parseColor("#32b16c"));
        clubJumper.setBackgroundColor(Color.parseColor("#32b16c"));
        messageJumper.setBackgroundColor(Color.parseColor("#32b16c"));
        userJumper.setBackgroundColor(Color.parseColor("#32b16c"));
        ((ImageView)findViewById(R.id.index_jumper_img)).setImageResource(R.drawable.index_bt_normal);
        ((TextView)findViewById(R.id.index_jumper_text)).setTextColor(Color.parseColor("#ffffff"));
        ((ImageView)findViewById(R.id.club_jumper_img)).setImageResource(R.drawable.bookclub_bt_normal);
        ((TextView)findViewById(R.id.club_jumper_text)).setTextColor(Color.parseColor("#ffffff"));
        ((ImageView)findViewById(R.id.message_jumper_img)).setImageResource(R.drawable.message_bt_normal);
        ((TextView)findViewById(R.id.message_jumper_text)).setTextColor(Color.parseColor("#ffffff"));
        ((ImageView)findViewById(R.id.user_jumper_img)).setImageResource(R.drawable.user_bt_normal);
        ((TextView)findViewById(R.id.user_jumper_text)).setTextColor(Color.parseColor("#ffffff"));
        switch (index){
            case 0:
                ((ImageView)findViewById(R.id.index_jumper_img)).setImageResource(R.drawable.index_bt_check);
                ((TextView)findViewById(R.id.index_jumper_text)).setTextColor(Color.parseColor("#32b16c"));
                indexJumper.setBackgroundColor(Color.parseColor("#ffffff"));
                break;
            case 1:
                ((ImageView)findViewById(R.id.club_jumper_img)).setImageResource(R.drawable.bookclub_bt_clicked);
                ((TextView)findViewById(R.id.club_jumper_text)).setTextColor(Color.parseColor("#32b16c"));
                clubJumper.setBackgroundColor(Color.parseColor("#ffffff"));
                break;
            case 2:
                ((ImageView)findViewById(R.id.message_jumper_img)).setImageResource(R.drawable.message_bt_check);
                ((TextView)findViewById(R.id.message_jumper_text)).setTextColor(Color.parseColor("#32b16c"));
                messageJumper.setBackgroundColor(Color.parseColor("#ffffff"));
                break;
            case 3:
                ((ImageView)findViewById(R.id.user_jumper_img)).setImageResource(R.drawable.user_bt_check);
                ((TextView)findViewById(R.id.user_jumper_text)).setTextColor(Color.parseColor("#32b16c"));
                userJumper.setBackgroundColor(Color.parseColor("#ffffff"));
                break;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void local(){
        final Integer[] resArray = new Integer[] {R.drawable.adv_page1, R.drawable.adv_page2,R.drawable.adv_page3};
        // 把数组交给图片展播组件
        indicate_view.setupLayoutByDrawable(resArray);
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.adv_page1);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);

        // 展播的风格
        //indicate_view.setIndicateStyle(ImageIndicatorView.INDICATE_ARROW_ROUND_STYLE);
        indicate_view.setIndicateStyle(ImageIndicatorView.INDICATE_USERGUIDE_STYLE);
        indicate_view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,metric.widthPixels*bitmap.getHeight()/bitmap.getWidth()));
        // 显示组件
        indicate_view.show();
        final AutoPlayManager autoBrocastManager = new AutoPlayManager(indicate_view);
        //设置开启自动广播
        autoBrocastManager.setBroadcastEnable(true);
        //autoBrocastManager.setBroadCastTimes(5);//loop times
        //设置开始时间和间隔时间
        autoBrocastManager.setBroadcastTimeIntevel(3000, 3000);
        //设置循环播放
        autoBrocastManager.loop();
    }

}
